package com.ssau.expert.core.util;

/**
 * Author: Sergey Saiyan
 * Created: 19.09.13 13:41
 */
public class StringHelper {

    public static int countOf(String destination, String target) {
        return countOf(destination, target, false);
    }

    public static int countOf(String destination, String target, boolean ignoreCase) {
        if (destination == null || target == null) {
            throw new NullPointerException("Input data is null");
        }

        if (destination.isEmpty() || destination.length() < target.length()) {
            return 0;
        }

        if (target.isEmpty()) {
            return -1;
        }

        int dLength = destination.length();
        int tLength = target.length();
        int count = 0;

        if (ignoreCase) {
            for (int i = 0; i < dLength - tLength + 1; i++) {
                String tmp = destination.substring(i, tLength + i);
                if (tmp.equalsIgnoreCase(target)) {
                    count++;
                }
            }
        } else {
            for (int i = 0; i < dLength - tLength + 1; i++) {
                String tmp = destination.substring(i, tLength + i);
                if (tmp.equals(target)) {
                    count++;
                }
            }
        }

        return count;
    }

    private StringHelper() {
    }
}
